package top.lingkang.fv.springmvc;

import org.springframework.validation.Errors;
import org.springframework.validation.SmartValidator;
import top.lingkang.finalvalidated.core.FinalValidatorFactory;
import top.lingkang.finalvalidated.error.CheckException;

/**
 * @author lingkang
 * created by 2025-11-10
 */
public class FinalValidatorFactorySpringMvc extends FinalValidatorFactory implements SmartValidator {

    @Override
    public void validate(Object target, Errors errors) {
        // 若已经积累了异常，将其抛出
        if (errors.hasErrors()) {
            throw new CheckException("校验参数前存在mvc解析异常" + errors)
                    .setErrorObject(errors);
        }

        // 校验
        validate(target);
    }

    @Override
    public void validate(Object target, Errors errors, Object... validationHints) {
        // 若已经积累了异常，将其抛出
        if (errors.hasErrors()) {
            throw new CheckException("校验参数前存在mvc解析异常" + errors)
                    .setErrorObject(errors);
        }

        // 校验、存在分组
        validate(target, (Class<?>[]) validationHints);
    }

    @Override
    public void validateValue(Class<?> targetType, String fieldName, Object value, Errors errors, Object... validationHints) {
        SmartValidator.super.validateValue(targetType, fieldName, value, errors, validationHints);
    }
}
